package com.example.config;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.example.globalcontrol.inteceptor.MyIncerteptor;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60*1000)
public class ApplicationConfig implements WebMvcConfigurer {
    @Bean
    public HttpMessageConverters fastJsonConfigure(){
        ParserConfig.getGlobalInstance().addAccept("com.example.");
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteDateUseDateFormat,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect);
        //日期格式化

        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");

        converter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(converter);
    }
    @Bean
    public TomcatServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
                connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
            }
        });
        return factory;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new MyIncerteptor()).addPathPatterns("/**").excludePathPatterns("/", "/login.html", "/user/login","/css/**","/fonts/**","/img/**","/js/**","/media/**");
    }

}
